package com.whq.thrift.api.web.admin.service.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.common.DateUtil;
import com.whq.thrift.api.common.constants.CommonConfigConstants;
import com.whq.thrift.api.common.spring.ReloadableConfManager;
import com.whq.thrift.api.web.admin.service.consumelog.ConsumeLogService;
import com.whq.thrift.api.web.admin.service.requestlog.RequestLogService;

@Service
public class ClearObsoleteDataJob {
	private static final Logger LOGGER = LoggerFactory.getLogger(MonitorConsumeLogInfoJob.class);
	private static final Logger LOGGER_QUARTZ = LoggerFactory.getLogger(CommonConfigConstants.LOGGER_KEY_QUARTZ);

	@Autowired
	private RequestLogService requestLogService;
	
	@Autowired
	private ConsumeLogService consumeLogService;

	@Autowired
	private ReloadableConfManager reloadableConfManager;

	public void clearObsoleteData() {
		int thresholdDay = reloadableConfManager.getIntPropertyValue("stat.delete.day.threshold", null, 20);

		// 获取当前时间日期
		long currentTimeMillis = System.currentTimeMillis();
		Date currentTime=new Date(currentTimeMillis);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date thresholdDate = DateUtil.getOffsetDateBegin(currentTime, -thresholdDay);
		String thresholdTime = format.format(thresholdDate);
		// 获取要删除的表数据的时间条件,删除20天之前的数据
		try {
			this.requestLogService.delHourApiData(thresholdTime);
		} catch (Exception e) {
			LOGGER.error("delete db_log_request.t_stat_hour_api_data requestLog exception:", e);
		}
		
		try{
			this.consumeLogService.delHourApiData(thresholdTime);
		}catch(Exception e){
			LOGGER.error("delete db_log_consume.t_stat_hour_api_data exception:", e);
		}
		
		LOGGER_QUARTZ.info("delete success");
	}
}
